﻿using System;
using System.IO;
using LitJson;
using viBlog.Classes;
using viBlog.Controllers;
using visinia.SmartEngine;
using System.Text.RegularExpressions;

public partial class ClientScripts_viBlog_Comments_SubmitComment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SaveComment();
    }

    private void SaveComment()
    {
        string json = Request.Params["data"];
        if (string.IsNullOrEmpty(json))
            return;
        CommentData commentdata = JsonMapper.ToObject<CommentData>(json);
        int postid = 0;
        if (Int32.TryParse(commentdata.postid, out postid))
        {
            Comment comment = new Comment();
            comment.PostId = postid;
            comment.Author = commentdata.author;
            comment.Website = commentdata.website;
            comment.Email = commentdata.email;
            comment.Content = ResolveLinks(commentdata.comment);
            comment.Date = DateTime.Now;
            if (BlogController.ModerateComments)
                comment.Status = Comment.CommentStatus.Pending;
            else
                comment.Status = Comment.CommentStatus.Approved;
            int commentid = CommentController.AddComment(comment);
            if (BlogController.ModerateComments)
            {
                Response.Clear();
                Response.Write("Comment will show up after Moderation");
                Response.End();
            }
            else
            {
                StringWriter writer = new StringWriter();
                Server.Execute(WebRoots.GetModulesRoot + "/viBlog/CommentsList/RenderComment.aspx?CommentId=" + commentid, writer);
                Response.Clear();
                Response.Write(writer.ToString());
                Response.End();
            }
        }        
    }

    private class CommentData
    {
        private string _postid;
        public string postid { get { return _postid; } set { _postid = value; } }

        private string _author;
        public string author { get { return _author; } set { _author = value; } }

        private string _website;
        public string website { get { return _website; } set { _website = value; } }

        private string _email;
        public string email { get { return _email; } set { _email = value; } }

        private string _comment;
        public string comment { get { return _comment; } set { _comment = value; } }
    }


    #region Utility
    private static Regex regex =
    new Regex("((http://|www\\.)([A-Z0-9.-]{1,})\\.[0-9A-Z?&=-_\\./]{2,})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

    /// <summary>
    /// To prevent negative pagerank add rel attribute of all the links to nofollow
    /// </summary>
    protected string ResolveLinks(string comment)
    {
        foreach (Match match in regex.Matches(comment))
        {
            if (!match.Value.Contains("://"))
                comment = comment.Replace(match.Value, regex.Replace(comment, "<a href=\"http://$1\" rel=\"nofollow\">$1</a>"));
            else
                comment = comment.Replace(match.Value, regex.Replace(comment, "<a href=\"$1\" rel=\"nofollow\">$1</a>"));
        }
        return comment;
    } 
    #endregion

}
